/**
 * 
 */
package cn.ad.server.common.utils.monitor.impl;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import cn.ad.server.common.utils.monitor.MonitorMessage;
import cn.ad.server.common.utils.monitor.Transaction;

/**
 * @author Wu Chengjun
 *
 */
public class DefaultTransaction implements Transaction {
	
	/**
	 * 名称
	 */
	private String name;

	/**
	 * 创建时间
	 */
	private long createtime;
	
	/**
	 * 开始时间
	 */
	private long starttime;
	
	/**
	 * 结束时间
	 */
	private long endtime;
	
	private Map<String, Object> data = new ConcurrentHashMap<>();

	private List<MonitorMessage> children;
	
	private TransactionCompleTrigger trigger;
	
	private String theadID;
	
	public DefaultTransaction() {
		this(null);
	}
	
	/**
	 * 
	 */
	public DefaultTransaction(String name) {
		createtime = System.currentTimeMillis();
		children = new LinkedList<>();
		this.name = name;
	}

	/* (non-Javadoc)
	 * @see cn.ad.server.web.utils.monitor.MonitorMessage#getTimestamp()
	 */
	@Override
	public long getTimestamp() {
		return createtime;
	}

	/* (non-Javadoc)
	 * @see cn.ad.server.web.utils.monitor.MonitorMessage#getType()
	 */
	@Override
	public String getType() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see cn.ad.server.web.utils.monitor.MonitorMessage#addContent(java.lang.String, java.lang.Object)
	 */
	@Override
	public void addContent(String key, Object value) {
		data.put(key, value);
	}

	/* (non-Javadoc)
	 * @see cn.ad.server.web.utils.monitor.MonitorMessage#getContents()
	 */
	@Override
	public Object getContents() {
		return data;
	}

	/* (non-Javadoc)
	 * @see cn.ad.server.web.utils.monitor.Transaction#start()
	 */
	@Override
	public void start() {
		this.starttime = System.currentTimeMillis();
	}

	/*
	 * (non-Javadoc)
	 * @see cn.ad.server.web.utils.monitor.Transaction#complete()
	 */
	@Override
	public void complete() {
		this.endtime = System.currentTimeMillis();
		if(trigger != null){
			trigger.trigger(this);
		}
	}

	/* (non-Javadoc)
	 * @see cn.ad.server.web.utils.monitor.Transaction#getName()
	 */
	@Override
	public String getName() {
		return name;
	}
	
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/* (non-Javadoc)
	 * @see cn.ad.server.web.utils.monitor.Transaction#getDuration()
	 */
	@Override
	public long getDuration() {
		return endtime - starttime;
	}

	/* (non-Javadoc)
	 * @see cn.ad.server.web.utils.monitor.Transaction#addChild(cn.ad.server.web.utils.monitor.MonitorMessage)
	 */
	@Override
	public Transaction addChild(MonitorMessage message) {
		children.add(message);
		return this;
	}

	/* (non-Javadoc)
	 * @see cn.ad.server.web.utils.monitor.Transaction#getChildren()
	 */
	@Override
	public List<MonitorMessage> getChildren() {
		return children;
	}
	
	public void setCompleteTrigger(TransactionCompleTrigger trigger){
		this.trigger = trigger;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(getName())
		  .append("\t")
		  .append(getDuration());
		List<MonitorMessage> msgs = getChildren();
		if(msgs.size() > 0){
			sb.append('\n');
		}
		for(MonitorMessage msg : msgs){
			sb.append(msg.toString());
			sb.append('\n');
		}
		return sb.toString();
	}
	
}
